package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.model.EbWorkDispatch;
import cn.com.chengmandian.bike.mapper.EbWorkDispatchMapper;
import cn.com.chengmandian.bike.service.IEbWorkDispatchService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 电单车运营-工作台-调度记录 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-10-25
 */
@Service
public class EbWorkDispatchServiceImpl extends ServiceImpl<EbWorkDispatchMapper, EbWorkDispatch> implements IEbWorkDispatchService {

    @Override
    public void addRideOrder(EbRideOrder ebRideOrder) {
        try {
            EbWorkDispatch workDispatch = this.getOne(new LambdaQueryWrapper<EbWorkDispatch>().eq(EbWorkDispatch::getBikeId, ebRideOrder.getBikeId())
                    .eq(EbWorkDispatch::getStatus, CommonConstant.TWO)
                    .orderByDesc(EbWorkDispatch::getId)
                    .last("limit 1"));
            if (workDispatch == null) return;
            if (workDispatch.getRideOrderId() != null) return;
            this.update(new LambdaUpdateWrapper<EbWorkDispatch>().set(EbWorkDispatch::getRideOrderId, ebRideOrder.getId())
                    .set(EbWorkDispatch::getRideOrderNo, ebRideOrder.getOrderNo())
                    .set(EbWorkDispatch::getRideOrderTime, ebRideOrder.getCreateTime())
                    .eq(EbWorkDispatch::getId,workDispatch.getId()));
        } catch (Exception e) {
            log.error("EbWorkDispatchService addRideOrder error!", e);
        }
    }
}
